/******************************************************************************\
*  Animeshell 1.0 Multimedia manager                                           *
*  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
*                                                                              *
*  This program is free software; you can redistribute it and/or modify        *
*  it under the terms of the GNU General Public License version 2 only as      *
*  published by the Free Software Foundation.                                  *
*                                                                              *
*  This program is distributed in the hope that it will be useful,             *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
*  GNU General Public License for more details.                                *
*                                                                              *
*  You should have received a copy of the GNU General Public License           *
*  along with this program; if not, write to the                               *
*  Free Software Foundation, Inc.,                                             *
*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
\******************************************************************************/

//
#include <QtGui>
#include <QMessageBox>

#include "rechercheexterne.h"
#include "imgcontrol.h"
//
rechercheexterne::rechercheexterne( QWidget *parent ) : QDialog(parent)
{
	// Setup the ui file
	setupUi(this);
	
	// Connect the button
	connect(btnSearch, SIGNAL(clicked()), this, SLOT(btnSearch_Clicked()));
	connect(btnClose, SIGNAL(clicked()), this, SLOT(btnClose_Clicked()));
}

void rechercheexterne::setWebSearch(QStringList pSearchList)
{
	cboWebSearch->addItems(pSearchList);
}

void rechercheexterne::setWebBrowser(QString pName)
{
	if ( pName != "" )
		m_Browser = pName;
}
	
	
void rechercheexterne::btnSearch_Clicked()
{
	// Variables
	QString name;
	QString arg;
	
	// Look if we have more than one word
	if ( txtSearchText->text().contains(QChar(' '), Qt::CaseInsensitive) )
	{
		name = CreateProperName( txtSearchText->text() );
	}
	else
	{
		name = txtSearchText->text();
	}
	
	// Which web search to use?
	if ( cboWebSearch->currentText() == "http://anidb.info" )
		arg = "http://anidb.info/perl-bin/animedb.pl?show=animelist&adb.search=" + name;
	else
	{
		if ( cboWebSearch->currentText() == "www.google.ca" )
			arg = "http://www.google.ca/search?hl=fr&q=" + name + "&btnG=Rechercher&meta=";
		else
			arg = "http://www.imdb.com/find?s=all&q=" + name;
	}
	
	// Exec the browser
	imgcontrol::startprocess(arg);
}
	
void rechercheexterne::btnClose_Clicked()
{
	close();
}

QString rechercheexterne::CreateProperName(QString pName)
{
	QString newname;
	
	newname = pName.replace( QChar(' '), QChar('_'), Qt::CaseInsensitive);
	
	return newname;
}
//
